package top.lingkang.finalgateway.utils;

import cn.hutool.core.io.IoUtil;
import top.lingkang.finalgateway.admin.vo.SslInfo;
import top.lingkang.finalgateway.bean.CertificateInfo;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Enumeration;

/**
 * @author lingkang
 * @create by 2024/5/14 10:53
 */
public class SslUtils {
    public static CertificateInfo readCertificateInfo(String publicKey) throws CertificateException {
        CertificateFactory factory = CertificateFactory.getInstance("X.509");
        Certificate cert = factory.generateCertificate(IoUtil.toUtf8Stream(publicKey));
        X509Certificate x509Cert = (X509Certificate) cert;
        CertificateInfo info = new CertificateInfo();
        info.setStartTime(x509Cert.getNotBefore());
        info.setEndTime(x509Cert.getNotAfter());
        info.setDomain(x509Cert.getSubjectDN().getName());
        info.setIssuer(x509Cert.getIssuerDN().getName());
        return info;
    }

    public static String getDomain(String cn) {
        if (cn.contains("=")) {
            cn = cn.substring(cn.indexOf("=") + 1);
            int index = cn.indexOf(",");
            if (index != -1)
                cn = cn.substring(0, index);
            if (cn.contains("Unknown"))
                return null;
            return cn;
        }
        return null;
    }

    public static SSLContext getSslContext(String password, InputStream jksIn) throws Exception {
        char[] passArray = password == null ? null : password.toCharArray();
        SSLContext sslContext = SSLContext.getInstance("SSL");
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(jksIn, passArray);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, passArray);
        sslContext.init(kmf.getKeyManagers(), null, null);
        jksIn.close();
        return sslContext;
    }

    /**
     * 读取jks中的信息
     */
    public static SslInfo getJksDomain(String password, InputStream jksIn) throws Exception {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(jksIn, password == null ? null : password.toCharArray());
        jksIn.close();
        Enumeration<String> aliases = ks.aliases();
        SslInfo info = new SslInfo();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (ks.isKeyEntry(alias)) {
                X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
                info.setDomain(cert.getSubjectDN().getName());
                info.setStartTime(cert.getNotBefore());
                info.setEndTime(cert.getNotAfter());
            }
        }
        return info;
    }

    /**
     * GET / HTTP/1.1
     * User-Agent: Post
     */
    public static String getPath(String oneLine) {
        String[] split = oneLine.split(" ");
        if (split.length > 1)
            return split[1];
        return "/";
    }

}
